var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var db = require('./model/db.js');
var cookieParser = require('cookie-parser')
var session=require('express-session')

//这些设定好之后post请求了就有了req.body了
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

app.use(cookieParser())
app.use(session({
    secret:'keyboard cat',
    resave:false,
    saveUninitialized:true,
}))



app.set('view engine', 'ejs');

app.use(express.static('./public'));


app.get('/login', function (req, res, next) {
    if(req.session.username){
            res.send(req.session.username)
    }else{
         res.render('denglu')
    }

   
})
app.post('/login', function (req, res, next) {
    var json={
        username:req.body.username,         //用户名
        password:req.body.password          //密码
    }
    db.find('eee',json,function(err,data){
      
        if(data.length!=0){
             req.session.username=data[0].username;         //设置session
                res.redirect('/login');     //登录成功后转发页面
        }else{
                res.send('登录失败')
        }
    })
})

app.listen(3000)